package com.jie.jie.xiaohua.ui;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;

import com.jaydenxiao.common.base.BaseFragment;
import com.jaydenxiao.common.commonutils.LogUtils;
import com.jaydenxiao.common.v.recyclerview.SwipeRecyclerView;
import com.jie.jie.R;
import com.jie.jie.xiaohua.app.XiaohuaRetrofitManager;
import com.jie.jie.xiaohua.contract.XiaohuaContract;
import com.jie.jie.xiaohua.model.XiaohuaBean;
import com.jie.jie.xiaohua.model.XiaohuaModel;
import com.jie.jie.xiaohua.presenter.XioahuaPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by jie on 2017/1/24.
 */

public class ImgFragment extends BaseFragment<XioahuaPresenter, XiaohuaModel> implements XiaohuaContract.View {
    @Bind(R.id.xiaohua_rv1)
    SwipeRecyclerView xiaohuaRv;
    private boolean updata;
    private XiaohuaRvAdapter adapter;
    private int updataPage, loadmorePage;
    private String time, firsttime;
    private List<XiaohuaBean.ResultBean.DataBean> datas;
    private Context context;

    @Override
    protected int getLayoutResource() {
        return R.layout.img_xiaohua;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    protected void initView() {
        XiaohuaRetrofitManager.BASE_URL = " http://japi.juhe.cn/joke/img/";
        context = getContext();
        long l = System.currentTimeMillis();
        time = String.valueOf(l);
        firsttime = time = time.substring(0, 10);
        LogUtils.loge(time);
        updataPage = 1;
        loadmorePage = 2;
        datas = new ArrayList<>();
        adapter = new XiaohuaRvAdapter(context, datas);
        xiaohuaRv.getRecyclerView().setLayoutManager(new LinearLayoutManager(context));
        xiaohuaRv.setAdapter(adapter);
        xiaohuaRv.setRefreshEnable(false);
        xiaohuaRv.setLoadMoreEnable(true);
        xiaohuaRv.setOnLoadListener(new SwipeRecyclerView.OnLoadListener() {
            @Override
            public void onRefresh() {
                mPresenter.getXiaohua("asc", time, updataPage, 10, true);
                time = String.valueOf(System.currentTimeMillis());
            }

            @Override
            public void onLoadMore() {
                mPresenter.getXiaohua("desc", firsttime, loadmorePage, 10, false);
                loadmorePage++;
            }
        });
        mPresenter.getXiaohua("desc", time, 1, 10, false);
    }

    @Override
    public void returnXioahua(XiaohuaBean xiaohuaBean, boolean updata) {
        if (updata) {
            datas.addAll(0, xiaohuaBean.getResult().getData());
            adapter.notifyDataSetChanged();
            xiaohuaRv.complete();
            xiaohuaRv.stopLoadingMore();
        } else {
            datas.addAll(xiaohuaBean.getResult().getData());
            adapter.notifyDataSetChanged();
            xiaohuaRv.complete();
            xiaohuaRv.stopLoadingMore();

        }
    }

    @Override
    public void showLoading(String title) {

    }

    @Override
    public void stopLoading() {

    }

    @Override
    public void showErrorTip(String msg) {

    }



}


